package org.blueoxygen.kukuruyuk.actions.address;
import java.lang.reflect.InvocationTargetException;
import java.sql.Timestamp;
import org.apache.commons.beanutils.PropertyUtils;
import org.blueoxygen.cimande.LogInformation;
import org.blueoxygen.cimande.gx.entity.GxDroplistValue;
import org.blueoxygen.kukuruyuk.entity.Address;
public class SaveAddress extends AddressForm {
	private String dropValue;
public String execute(){
		if(getAddress().getCompanyname() == null ||
				"".equalsIgnoreCase(getAddress().getCompanyname())){
				addActionError("Nama Perusahaan tidak boleh kosong");
		}
		if(getAddress().getAddress() == null ||
				"".equalsIgnoreCase(getAddress().getAddress())){
				addActionError("Alamat tidak boleh kosong");
		}
		if(hasErrors()){
			setDroplistValues(manager.getList("SELECT w FROM "+GxDroplistValue.class.getName()+" w WHERE w.name.id='8a8a8a941f59f569011f59f6372c0001'", null,null));
			return INPUT;
		}
		LogInformation log;
		if(getAddress().getId() == null || "".equalsIgnoreCase(getAddress().getId())){
			log = new LogInformation();
			log.setCreateBy(sessionCredentials.getCurrentUser().getId());
			log.setCreateDate(new Timestamp(System.currentTimeMillis()));
			getAddress().setId(null);
		} else {
			Address temp = getAddress();
			setAddress((Address) manager.getById(Address.class, getAddress().getId()));
			log = getAddress().getLogInformation();
			try {
				PropertyUtils.copyProperties(getAddress(), temp);
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			} catch (InvocationTargetException e) {
				e.printStackTrace();
			} catch (NoSuchMethodException e) {
				e.printStackTrace();
			}
		}
		log.setLastUpdateBy(sessionCredentials.getCurrentUser().getId());
		log.setLastUpdateDate(new Timestamp(System.currentTimeMillis()));
		log.setActiveFlag(1);
		getAddress().setLogInformation(log);
		getAddress().setType(getDropValue());
		manager.save(getAddress());
		return SUCCESS;
	
	}
public String getDropValue() {
	return dropValue;
}
public void setDropValue(String dropValue) {
	this.dropValue = dropValue;
}
}	

